//获取工具类的应用实例  
const util = require('../../../utils/util.js');
const api = require('../../../utils/api.js');

const App = getApp();

Page({
  data: {
    //页面适配
    imgwidth: 0,
    imgheight: 0,
    windowWidth: wx.getSystemInfoSync().screenWidth,
    windowHeight: wx.getSystemInfoSync().windowHeight,
    //wxml渲染层参数
    goods_data: {
      list: [],
      page: {
        page_no: 0,
        page_size: 6,
      },
      paginate: {}
    }
  },

  //初始化数据加载
  onLoad() {
    this.getNewPeopleList();
  },



  //加载新人专享列表分页
  getNewPeopleList() {
    const self = this;
    
    let pageData = {
      page_no: this.data.goods_data.page.page_no + 1,
      page_size: this.data.goods_data.page.page_size,
    }

    let data = {
      page: JSON.stringify(pageData),
      service: 'psGoods.smallprogram.freeGetGoodsByLabelListPageFind',
      params: '{"label_promotion":"freshman"}',

      url_type: 'goods',

      app_token: App.globalData.app_token,
      security_code: App.globalData.security_code
    }
    
    var goods_data = this.data.goods_data

    api.reqData({
      data,
      success: (res) => {
        console.log(res.data.data)


        //图片地址由https 改为 http
        var goodlist = res.data.data.list
        for (var i = 0; i < goodlist.length; i++) {
          goodlist[i].pic_info_url = goodlist[i].pic_info_url.replace("https", "http")
        }

        goods_data.list = [...goods_data.list, ...goodlist]
        goods_data.page.page_no = res.data.data.page.page_no
        goods_data.page.page_size = res.data.data.page.page_size
        //paginate.total_page = res.data.data.page.total_page
        goods_data.paginate.total_count = res.data.data.page.total_count
        //判断hasNext
        if (goods_data.paginate.total_count > goods_data.page.page_no * goods_data.page.page_size) {
          goods_data.paginate.hasNext = true
        } else {
          goods_data.paginate.hasNext = false
        }
        this.setData({
          goods_data: goods_data,
        })
      }
    })
  },


  //拉到底部刷新
  onPullDownRefresh() {
    console.info('onPullDownRefresh')
    if (!this.data.goods_data.paginate.hasNext) return
    this.getNewPeopleList()
  },
  
  toDetail: function (e) {

    let source = e.currentTarget.dataset.source;
    let goods_id = e.currentTarget.id;

    if (source == 'meitianhui') {
      wx.navigateTo({
        url: `../../buy/detail/selfEmployed/selfEmployed?goods_id=` + goods_id,
      });
    } else {
      wx.navigateTo({
        url: `../../buy/detail/taotaoling/taotaoling?goods_id=` + goods_id,
      });
    }

  },


  //商品图片适配
  imageLoad: function (e) {
    var windowWidth = this.data.windowWidth;
    //新人专享列表查询图片宽高：589 855
    var $width = e.detail.width;

    //设置图片显示宽度,适配0.92的比例，
    var viewWidth = windowWidth;
    var viewHeight = viewWidth;    //计算的高度值     
    this.setData({
      imgwidth: viewWidth,
      imgheight: viewHeight
    })
  },

})